package com.bw.house.vm

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bw.common.entity.BaseRes
import com.bw.common.vm.BaseViewModel
import com.bw.house.entity.HouseEntityItem
import com.bw.house.entity.TenantEntityItem
import com.bw.house.model.HouseRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject
@HiltViewModel
class HouseViewModel @Inject constructor(private val repo:HouseRepository):BaseViewModel() {
    val mHouseSuccess = MutableLiveData<List<HouseEntityItem>>()
    fun  getHouseData(id:Int){
        viewModelScope.launch (mDispatcher){
            val res = repo.loadHouseData(id)
            if (res.code ==200){
                mHouseSuccess.postValue(res.data)
            }else{
                mError.postValue("请求失败${ res.message }" )
            }

        }
    }

    val mTenantSuccess = MutableLiveData<List<TenantEntityItem>>()
    fun getTenantData(){
        viewModelScope.launch(mDispatcher) {
            val res = repo.loadTenantData()
            if (res.code ==200){
                mTenantSuccess.postValue(res.data)
            }else{
                mError.postValue("请求失败${ res.message }" )
            }
        }
    }

    val mSearchSuccess = MutableLiveData<List<TenantEntityItem>>()
    fun getSearchData(keyword:String){
        viewModelScope.launch(mDispatcher) {
            val res = repo.loadSearchData( keyword)
            if (res.code == 200){
                mSearchSuccess.postValue(res.data)
            }else{
                mError.postValue("请求失败${ res.message }" )
            }
        }
    }

}